<?php

namespace app\api\controller\xykeep;

use app\common\controller\Api;
use app\common\model\News as NewsModel;
use app\common\model\NewsComment;
use app\common\model\NewsLike;
use app\common\validate\NewsComment as NewsCommentValidate;
use think\Config;


class News extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function lists()
    {
        $params = $this->request->param();
        $where = [];
        if($params['cate_id']){
            $where['category_id'] = $params['cate_id'];
        }
        $data = NewsModel::where($where)->paginate(10);
        foreach($data as $k=>&$v){
            $v['desc'] = mb_substr(strip_tags($v['content']), 0, 40, "UTF-8");
        }
        $this->success('success', $data);
    }

    public function detail()
    {
        $params = $this->request->param();
        $user_id = $this->auth->id;
        $where = [
            'id' => $params['id'],
        ];
        $data = NewsModel::where($where)->find();

        NewsModel::where($where)->setInc('views');

        $data['comment'] = (new NewsComment())->with('user')->where([
            'news_id' => $params['id'],
        ])->select();

        $data['isLike'] = (new NewsLike())->where([
            'news_id' => $params['id'],
            'user_id' => $user_id
        ])->count();


        $this->success('success', $data);
    }


    //提交新闻评价
    public function comment()
    {
        $param = $this->request->post();
        $user_id = $this->auth->id;
        $param['user_id'] = $user_id;

        $validate = new NewsCommentValidate();
        if (!$validate->scene('add')->check($param)) {
            $this->error($validate->getError());
        }

        $commentModel = new NewsComment();
        $commentModel->save([
            'user_id' => $user_id,
            'news_id' => $param['news_id'],
            'content' => $param['content']
        ]);

        $this->success('评论成功');
    }

    //点赞
    public function like()
    {
        $param = $this->request->post();
        $user_id = $this->auth->id;
        $NewsLikeModel = new NewsLike();
        $find = $NewsLikeModel->where([
            'user_id' => $user_id,
            'news_id' => $param['news_id']
        ])->find();
        if ($find) {
            $NewsLikeModel->where([
                'user_id' => $user_id,
                'news_id' => $param['news_id']
            ])->delete();
            $this->success('取消点赞',0);
        }else{
            $NewsLikeModel::create([
                'user_id' => $user_id,
                'news_id' => $param['news_id']
            ]);
            $this->success('点赞',1);
        }

    }


    /**
     * 其他
     */
    public function other()
    {
        $type = $this->request->get('type');
        $info = [];
        if($type=='privacy'){
            $info['title'] = '隐私协议';
            $info['content'] = Config::get('site.privacy');
        }else if($type=='question') {
            $info['title'] = '常见问题';
            $info['content'] = Config::get('site.question');
        }
        $this->success('其他',$info);
    }
}